package com.gzdh.tang.dishrecognition.service.impl;

import com.gzdh.tang.config.leshua.LeshuaPayUtils;
import com.gzdh.tang.config.leshua.leshuaUtil.HttpClientHelper;
import com.gzdh.tang.config.leshua.leshuaUtil.LeshuaSignature;
import com.gzdh.tang.config.leshua.leshuaUtil.XmlUtils;
import com.gzdh.tang.dishrecognition.service.LsPayService;
import com.gzdh.tang.config.leshua.leshuaUtil.SignUtils;
import com.gzdh.tang.config.leshua.leshuaUtil.MD5;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Service;
import java.util.*;

@Service("lsPayService")
@Slf4j
public class LsPayServiceImpl implements LsPayService {

    private final HttpClientHelper httpClientHelper = new HttpClientHelper();

    @Override
    public Map<String, String> authCodePay(String authCode, String outTradeNo, String payAmount, String merchantId, String key) {
        log.info("扫码支付...");
        SortedMap<String, String> map = new TreeMap<>();
        map.put("service", "upload_authcode");
        map.put("third_order_id", outTradeNo);
        map.put("amount", payAmount);
        map.put("auth_code", authCode);
        map.put("merchant_id", merchantId);
        map.put("nonce_str", String.valueOf(new Date().getTime()));
        Map<String, String> params = SignUtils.paraFilter(map);
        StringBuilder buf = new StringBuilder((params.size() + 1) * 10);
        SignUtils.buildPayParams(buf, params, false);
        String preStr = buf.toString();
        log.info("preStr:{}",preStr);
        String sign = MD5.sign(preStr, "&key=" + key, "utf-8");
        map.put("sign", sign);
        String requestUrl = LeshuaPayUtils.getRequestUrl() + "/cgi-bin/lepos_pay_gateway.cgi";
        log.info("requestUrl:" + requestUrl);
        String res = httpClientHelper.postForm(requestUrl, map, HttpClientHelper.ACCEPT_TYPE_X_WWW);
        log.info("请求结果：" + res);
        try {
            return LeshuaSignature.getMapFromXML(res);
        } catch (Exception e) {
            e.printStackTrace();
            log.info("转化xml文件失败：" + e.toString());
        }
        return null;
    }

    @Override
    public Map<String, String> tradeOrderQuery(String outTradeNo, String merchantId, String key) {
        log.debug("订单查询...");
        SortedMap<String, String> map = new TreeMap<>();
        map.put("service", "query_status");
        map.put("merchant_id", merchantId);
        map.put("third_order_id", outTradeNo);
        map.put("nonce_str", String.valueOf(new Date().getTime()));
        String reqUrl = LeshuaPayUtils.getRequestUrl() + "/cgi-bin/lepos_pay_gateway.cgi";
        Map<String, String> params = SignUtils.paraFilter(map);
        StringBuilder sb = new StringBuilder((params.size() + 1) * 10);
        SignUtils.buildPayParams(sb, params, false);
        String preStr = sb.toString();
        String sign = MD5.sign(preStr, "&key=" + key, "utf-8");
        map.put("sign", sign);
        log.debug("reqUrl:" + reqUrl);
        log.debug("reqParams:" + XmlUtils.parseXML(map));
        String res =  httpClientHelper.postForm(reqUrl, map, HttpClientHelper.ACCEPT_TYPE_X_WWW);
        try {
            boolean flag = LeshuaSignature.checkIsSignValidFromResponseString(res, LeshuaSignature.resExcludedSignParams(), key);
            log.info("验签结果:{}", flag);
        } catch (Exception e) {
            e.printStackTrace();
        }
        log.debug("请求结果：" + res);
        try {
            log.info("map:{}", LeshuaSignature.getMapFromXML(res));
            return LeshuaSignature.getMapFromXML(res);
        } catch (Exception e) {
            e.printStackTrace();
            log.info("转化xml文件失败：" + e.toString());
        }
        return null;
    }

    @Override
    public Map<String, String> lsRefund(String outTradeNo, String outRefundNo, String refundAmount, String merchantId, String key) {
        log.debug("退款...");
        SortedMap<String, String> map = new TreeMap<>();
        map.put("service", "unified_refund");
        map.put("merchant_id", merchantId);
        map.put("third_order_id", outTradeNo);
        map.put("merchant_refund_id", outRefundNo);
        map.put("refund_amount", refundAmount);
        map.put("nonce_str", String.valueOf(new Date().getTime()));
        Map<String, String> params = SignUtils.paraFilter(map);
        StringBuilder sb = new StringBuilder((params.size() + 1) * 10);
        SignUtils.buildPayParams(sb, params, false);
        String preStr = sb.toString();
        String sign = MD5.sign(preStr, "&key=" + key, "utf-8");
        map.put("sign", sign);
        String requestUrl = LeshuaPayUtils.getRequestUrl() + "/cgi-bin/lepos_pay_gateway.cgi";
        log.debug("reqUrl:" + requestUrl);
        String res = httpClientHelper.postForm(requestUrl, map, HttpClientHelper.ACCEPT_TYPE_X_WWW);
        log.debug("请求结果：" + res);
        try {
            return LeshuaSignature.getMapFromXML(res);
        } catch (Exception e) {
            e.printStackTrace();
            log.info("转化xml文件失败：" + e.toString());
        }
        return null;
    }
}
